package com.MonkWorks.timedormitory;

import java.util.Random;

import com.MonkWorks.clockDTO.Job;
import com.MonkWorks.clockDTO.User;
import com.MonkWorks.timedormitory.R;
import com.MonkWorks.timedormitoryDB.ItimeProvider;
import com.MonkWorks.timedormitoryDB.TimeDB;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ClientActivity extends Activity {
	// bring in persistence interface
	ItimeProvider timeUser;

	private Button button_Back;
	private Button button_Add;
	private Button btnFind;
	private EditText etAddress;
	private EditText etJob;
	private EditText etDesc;
	private int longitude;
	private int latitude;
	private String address;
	private String job;
	private String description;
	private String clientName;

	private void initComponent() {
		// implement timeDB
		timeUser = new TimeDB(this);

		button_Back = (Button) findViewById(R.id.btnBack);
		etAddress = (EditText) findViewById(R.id.etAddress);
		button_Add = (Button) findViewById(R.id.btnAdd);
		etJob = (EditText) findViewById(R.id.etJob);
		etDesc = (EditText) findViewById(R.id.etDescription);
		btnFind = (Button) findViewById(R.id.btnFind);

		// get Extras from other classes
		getExtras();

		etJob.setText(job);
		etAddress.setText(address);
		button_Back.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent BackIntent = new Intent(v.getContext(),
						PunchActivity.class);
				BackIntent.putExtra("username", clientName);
				startActivityForResult(BackIntent, 0);

			}
		});

		button_Add.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				//validates the text fields to see if blank or not
				if( etJob.getText().toString().length() == 0 ) { 
					etJob.setError( "Job name is required!" );
				}
				if( etAddress.getText().toString().length() == 0 ) { 
					etAddress.setError( "Address is required!" );
				}
				if( etDesc.getText().toString().length() == 0 ) { 
					etDesc.setError( "Description is required!" );
				}
				else if (etJob.getText().toString().length() != 0 && etAddress.getText().toString().length() != 0  &&
						etDesc.getText().toString().length() != 0	){
					
				
				Intent i = new Intent(v.getContext(), PunchActivity.class);
				i.putExtra("longitude", longitude);
				i.putExtra("latitude", latitude);
				i.putExtra("username", clientName);
				i.putExtra("address", address);
				startActivity(i);

				final Job job = new Job();
				job.setName(etJob.getText().toString());
				job.setAddress(etAddress.getText().toString());
				job.setDescription(etDesc.getText().toString());
				final User user = new User();
				user.setUsername(clientName);
				// add job
				try {
					popup("Job Saved");
					timeUser.saveJob(job, user);

					Log.i("ClientActivity", "Save Job");
				} catch (Exception e) {
					popup("Cannot create Job");
				}
			}

			}
		});

		/**
		 * Jacob Morgan When radio button is clicked a gps view is open where
		 * the use can add the long and lat coordinates
		 */
		btnFind.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				address = etAddress.getText().toString();
				job = etJob.getText().toString();
				//validates the text fields to see if blank or not
				if( etJob.getText().toString().length() == 0 ) { 
					etJob.setError( "Job name is required!" );
				}
				if( etAddress.getText().toString().length() == 0 ) { 
					etAddress.setError( "Address is required!" );
				}
				else if (etJob.getText().toString().length() != 0 &&
						etAddress.getText().toString().length() != 0){
				Intent i = new Intent(v.getContext(),
						ShowLocationActivity.class);
				i.putExtra("clientName", clientName);
				i.putExtra("address",address);
				i.putExtra("job",job);
				startActivity(i);
				}

			}
		});
	}

	private void getExtras() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			clientName = extras.getString("clientName");
			longitude = extras.getInt("longitude");
			latitude = extras.getInt("latitude");
			job = extras.getString("job");
			address = extras.getString("address");
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_client);
		initComponent();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_client, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    
	      //Logs the user out
	      if (item.getItemId() == R.id.Logoff) {
	    	  
	    	  Intent  MIntent = new Intent(getBaseContext(),LoginActivity.class);
				startActivityForResult(MIntent, 0);
				popup("You have logged out");
	        return true;
	      }


	      return (super.onOptionsItemSelected(item));
	    }

	protected void popup(String message) {
		// this is how we make a popup.
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
}
